<template>
  <div class="app-container">
    <div class="explain">1.提现必须绑定微信，否则无法发放红包；2.可设置最低提现金额(微信商户限制，最低金额0.3，最高200)；3.如需使用最低0.3需在微信商户中申请开通服务；4.同一用户每天的提现次数请在微信商户中配置。</div>
    <div class="spaceline" />
    <el-form
      ref="form"
      v-loading="infoLoading"
      class="common-form"
      :model="form"
      label-width="150px"
      :rules="rules"
      :inline-message="true"
    >
      <el-form-item label="企业认证提现设置">
        <el-switch v-model="form.enterprise_certification" />
        <span class="smalltip">
            <i class="el-icon-info" />
            需要通过营业执照认证
        </span>
      </el-form-item>
      <el-form-item label="最低提现金额限制">
        <el-switch v-model="form.amount_limit" />
        <span class="smalltip">
            <i class="el-icon-info" />
             关闭后将不限制金额
        </span>
      </el-form-item>
      <el-form-item label="最低提现金额设置">
        <el-input v-model="form.amount_setting" />
      </el-form-item>
      <el-form-item
        label="提现规则"
        prop="statistics"
      >
        <el-input
          v-model="form.tips"
          class="middle"
          type="textarea"
          :rows="4"
        />
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="onSubmit('form')">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { setConfig } from '@/api/configuration'

export default {
  data() {
    return {
      infoLoading: true,
      form: {
        enterprise_certification: false,
        amount_limit: false,
        amount_setting: '1',
        tips:''
      },
      rules: {}
    }
  },
  created() {
    this.fetchInfo()
  },
  methods: {
    fetchInfo() {
      this.infoLoading = true
      const param = {}
      setConfig(param, 'get')
        .then(response => {
          console.log(response.data.withdrawal)
          if(response.data.withdrawal.enterprise_certification == 1){
              this.form.enterprise_certification = true;
          }else{
              this.form.enterprise_certification = false;
          }
          if(response.data.withdrawal.amount_limit == 1){
            this.form.amount_limit = true;
          }else{
            this.form.amount_limit = false;
          }
          this.form.amount_setting = response.data.withdrawal.amount_setting;
          this.form.tips = response.data.withdrawal.tips;
          this.infoLoading = false
        })
        .catch(() => {})
    },
    onSubmit(formName) {
      const insertData = { ...this.form }
      this.$refs[formName].validate(valid => {
        if (valid) {
          insertData.is_open = insertData.is_open === true ? 1 : 0
          setConfig({'withdrawal':insertData})
            .then(response => {
              this.$store.dispatch('config/getConfigInfo')
              this.$message.success(response.message)
              return true
            })
            .catch(() => {})
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
.explain{
  padding: 20px 16px;
  background-color: #fdf6ec;
  border-radius: 4px;
  border-left: 5px solid #f9c77c;
  margin: 0 0 20px;
  font-size: 14px;
  color: #e6a23c;
  line-height: 1em;
}
.el-input,
.el-input-group {
  width: 50%;
}
</style>
